package gui.edit.listeners;

import gui.edit.components.A_ParamsSliders_panel;

import java.util.Dictionary;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import tuio.components.SliderTouch;

import data.Constants;

/**
 * @author Charles Perin
 * 29 mars 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 *
 */
public class ListenerParamSliderChange extends ListenerParamChange implements ChangeListener {

	public ListenerParamSliderChange(A_ParamsSliders_panel parent){
		super(parent);
	}

	@Override
	public void stateChanged(ChangeEvent e) {
		SliderTouch source = (SliderTouch) e.getSource();
		@SuppressWarnings("unchecked")
		Dictionary<Integer, JLabel> labelTable = source.getLabelTable();
		int sliderValue = source.getValue();
		String sliderValueString;
		if(labelTable!=null && labelTable.get(sliderValue)!=null){
			sliderValueString = labelTable.get(sliderValue).getText();
			if(sliderValueString.length()>Constants.CAMERA_PANEL_NB_LETTERS_FOR_SLIDERS_LABEL_TABLE){
				sliderValueString=sliderValueString.substring("<htlm><center>".length(),Constants.CAMERA_PANEL_NB_LETTERS_FOR_SLIDERS_LABEL_TABLE+"<html><center>".length());
			}

			((A_ParamsSliders_panel)parent).getField().setText(sliderValueString);
			((A_ParamsSliders_panel)parent).getField().setForeground(labelTable.get(sliderValue).getForeground());
			((A_ParamsSliders_panel)parent).getField().setBorder(BorderFactory.createLineBorder(((A_ParamsSliders_panel)parent).getField().getForeground()));
		}
		else{
			sliderValueString = Integer.toString(sliderValue);
			((A_ParamsSliders_panel)parent).getField().setText(sliderValueString);
		}

		
		if(!source.getValueIsAdjusting()){
			((A_ParamsSliders_panel)parent).getControler().getParamsControl().modifyParam(((A_ParamsSliders_panel)parent).getControler(), source);
		}
	}
}
